
const { controller_save,
    controller_find, controller_delete,
    controller_get } = require("../../../controller/context/article/ARTICLE.CONTROLLER");
const { serverError } = require("../../../utils");
const { emptyCheck } = require("../../../utils/checkEmpty");

/**
 * @description 文章列表路由
 * @author 韩
 */
const router = require("koa-router")()
router.prefix("/context/article");


/**@description 添加或编辑 */
router.post("/create", async (ctx, next) => {
    try {
        const {        // shopId,
            id = undefined,
            title = undefined,
            intro = undefined,
            subjectId = undefined,
            masterImage = [],
            description = undefined,
            status = undefined,
            sort = undefined,
            seoDescription = undefined,
            seoTitle = undefined,
            urlname = undefined,
            keyWords = undefined,
        } = ctx.request.body
        const check = emptyCheck({ title, description })
        if (check.code !== 0) {
            ctx.body = check
        } else {
            const res = await controller_save({
                id, title, intro, subjectId, masterImage: JSON.stringify(masterImage), description, status,
                seoDescription,
                seoTitle,
                urlname,
                keyWords,
            })
            ctx.body = res
        }
    } catch (error) {
        console.log(error)
        ctx.body = serverError
    }
})


/**@description 根据id获取分类详情 */
router.get("/get", async (ctx, next) => {
    try {
        const { id = undefined, title = undefined } = ctx.query
        const res = await controller_get({ id, title });
        // console.log("查询：\n", id)
        ctx.body = res;
    } catch (error) {
        ctx.body = serverError
    }
})

/**@description 根据id删除分类 */
router.delete("/delete", async (ctx, next) => {
    try {
        const id = ctx.query.id || undefined;
        // console.log("拿到id",ctx.query.id)
        const res = await controller_delete({ id: id });
        ctx.body = res;
    } catch (error) {
        console.log("删除出错")
        console.log("删除出错", error)
        ctx.body = serverError
    }
})


// 查询列表
router.post("/list", async (ctx, next) => {
    try {
        const { current, pageSize, title, status } = ctx.request.body;

        const res = await controller_find({
            current, pageSize, title, status
        })

        ctx.body = res
    } catch (error) {
        console.log(error)
        ctx.body = serverError
    }

})




module.exports = router